#!/bin/zsh

dev_account="ranganath.atreya@gmail.com"
dev_team="2P8V429HRL"
dev_keychain_label="Developer-altool"

# functions
requeststatus() { # $1: requestUUID
    requestUUID=${1?:"need a request UUID"}
    req_status=$(xcrun altool --notarization-info "$requestUUID" \
                              --username "$dev_account" \
                              --password "@keychain:$dev_keychain_label" 2>&1 \
                 | awk -F ': ' '/Status:/ { print $2; }' )
    echo "$req_status"
}

notarizefile() { # $1: path to file to notarize, $2: identifier
    filepath=${1:?"need a filepath"}
    identifier=${2:?"need an identifier"}

    # if app then zip before uploading
    filename=$(basename -- "$filepath")
    if [[ ${filename##*.} == "app" ]]; then
        ditto -c -k --keepParent "$filename" "${filename%.*}.zip"
        filename="${filename%.*}.zip"
    fi
    
    # upload file
    echo "## uploading $filepath for notarization"
    requestUUID=$(xcrun altool --notarize-app \
                               --primary-bundle-id "$identifier" \
                               --username "$dev_account" \
                               --password "@keychain:$dev_keychain_label" \
                               --asc-provider "$dev_team" \
                               --file "${filename}" 2>&1 \
                  | awk '/RequestUUID/ { print $NF; }')
                               
    echo "Notarization RequestUUID: $requestUUID"
    
    if [[ $requestUUID == "" ]]; then
        echo "could not upload for notarization"
        exit 1
    fi
        
    # wait for status to be not "in progress" any more
    request_status="in progress"
    while [[ "$request_status" == "in progress" ]]; do
        echo -n "waiting... "
        sleep 10
        request_status=$(requeststatus "$requestUUID")
        echo "$request_status"
    done
    
    # print status information
    xcrun altool --notarization-info "$requestUUID" \
                 --username "$dev_account" \
                 --password "@keychain:$dev_keychain_label"
    echo
    
    if [[ $request_status != "success" ]]; then
        echo "## could not notarize $filepath"
        exit 1
    fi
    
    # staple result
    echo "## Stapling $filepath"
    xcrun stapler staple "$filepath"
}

clean() {
    rm -rf ./VarnamIME.app
    rm -rf ./VarnamIME.zip
    rm -rf ./VarnamApp.app
    rm -rf ./VarnamApp.zip
    rm -rf ./VarnamIME.pkg
    rm ./Scripts/installer
}

build_varnam_ime() {
    rm -rf ./VarnamIME.app
    xcodebuild -project ../VarnamIME.xcodeproj -scheme VarnamIME -configuration Release clean build || exit 1
    cp -R ../build/Release/VarnamIME.app .
    # notarizefile "VarnamIME.app" "com.varnamproject.mac.Varnam"
}

build_shortcut_recorder() {
    xcodebuild -project ../ShortcutRecorder/ShortcutRecorder.xcodeproj -scheme "ShortcutRecorder.framework" -configuration Release clean build || exit 1
    rm -rf ../Application/ShortcutRecorder.framework
    cp -R ../ShortcutRecorder/build/Release/ShortcutRecorder.framework ../Application/ || exit 1
}

build_varnam_app() {
    rm -rf ./VarnamApp.app
    xcodebuild -project ../VarnamIME.xcodeproj -scheme VarnamApp -configuration Release clean build || exit 1
    cp -R ../build/Release/VarnamApp.app .
    # notarizefile "VarnamApp.app" "com.varnamproject.mac.VarnamApp"
}

build_varnam_installer() {
    rm ./Scripts/installer
    xcodebuild -project ../VarnamIME.xcodeproj -scheme Installer -configuration Release clean build  || exit 1
    cp ../build/Release/installer ./Scripts/
}

build_pkg() {
    packagesbuild -v VarnamIME.pkgproj
    # notarizefile "VarnamIME.pkg" "com.varnamproject.mac.VarnamApp"
}

if [[ "$1" == "clean" ]]; then
    clean
    exit 0
fi

if [[ "$1" == "ime" ]]; then
    rm -rf ./VarnamIME.zip
    build_varnam_ime
    exit 0
fi

if [[ "$1" == "app" ]]; then
    rm -rf ./VarnamApp.zip
    build_varnam_app
    exit 0
fi

if [[ "$1" == "ShortcutRecorder" ]]; then
    build_shortcut_recorder
    exit 0
fi

if [ ! -d ../Application/ShortcutRecorder.framework ]; then
    build_shortcut_recorder
fi

build_varnam_ime
build_varnam_app
build_varnam_installer
build_pkg

exit 0
